/***************************************************************************
 *   Copyright (C) 2009 by Docteur Pierre-Marie Desombre.   *
 *   pm.desombre@medsyn.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 3 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "textparser.h"
#include <common/settings.h>
#include <QMessageBox>
#include <QtCore>
#include <QtXml>

static inline QString textsFilePath() {
    settings set;
    return set.applicationPath()+"/../resources/txt";}


textParser::textParser(){

}

textParser::~textParser(){}

QHash<QString,QString> textParser::readTextFile(){
  QHash<QString,QString> hashTags;
  QString exchangePath = textsFilePath()+"/textsFile.txt";
  QFile textsFile(exchangePath);
  if(!textsFile.open(QIODevice::ReadOnly)){
      QMessageBox::warning(0,trUtf8("Error"),trUtf8("textsFile.txt not found."),QMessageBox::Ok);
      hashTags.insert("error","error");
    }
  QTextStream stream(&textsFile);
  stream.setCodec("Utf-8");
  QString tagName;
  QString text;
  while(!stream.atEnd()){
       QString str;
       qDebug() << __FILE__ << QString::number(__LINE__) ;
       str = QString("%1").arg(stream.readLine());
       if(str.contains("[") && str.contains("]")){ 
           text = ""; 
           tagName = "";
           tagName = str.remove("[").remove("]").trimmed();
              }
       else{
           text += str.trimmed();
           hashTags.insert(tagName,text);
          //qDebug() << "ba ="+str+" tag = "+tagName;
       }
     }
  QHashIterator<QString,QString> it(hashTags);
  while(it.hasNext()){
      it.next();
      qDebug() << __FILE__ << " it = " << it.key() << " + " << it.value();
  }
  return hashTags;
}
